import scrapy


class QuoteSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        "http://quotes.toscrape.com/page/1/",
        "http://quotes.toscrape.com/page/2/",
    ]
    # def start_requests(self):
    #     urls = [
    #         "http://quotes.toscrape.com/page/1/",
    #         "http://quotes.toscrape.com/page/2",
    #     ]
    # for url in urls:
    #     yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        # page = response.url.split("/")[-2]
        # file_name = "quotes-%s.html" % page
        # with open(file_name, "wb") as f:
        #     f.write(response.body)
        # self.log("Saved file %s" % file_name)
        for quote in response.css("div.quote"):
            yield {
                "text": quote.css("span.text::text").extract_first(),
                "author": quote.css("span small::text").extract_first(),
                "tags": quote.css("div.tags a.tag::text").extract(),
            }

        next_page = response.css("li.next a::attr(href)").extract_first()
        if next_page is not None:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)